/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved.
 */
package com.icinfo.cloud.provider.punish.ucase.casemain.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.common.exception.BusinessException;
import com.icinfo.cloud.provider.punish.ucase.casemain.mapper.CaseHandlePersonRecordMapper;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CaseHandlePersonRecord;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseHandlePersonRecordService;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.CaseHandlePersonRecordVo;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述: 案件办案人员记录表-TRJ case_handle_person_record 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2022年02月15日
 */
@Service
public class CaseHandlePersonRecordServiceImpl extends MyBatisServiceSupport implements ICaseHandlePersonRecordService {
    /**
     * 日志记录器
     */
    private static final Logger logger = LoggerFactory.getLogger(CaseHandlePersonRecordServiceImpl.class);

    @Autowired
    private CaseHandlePersonRecordMapper caseHandlePersonRecordMapper;

    /**
     * 描述：【V1.0.0】根据案件id获取办案人员信息记录
     *
     * @param caseId
     * @return
     * @author tianrunjia
     */
    @Override
    public List<CaseHandlePersonRecordVo> getCaseHandlePersonRecordListByCaseId(String caseId) {
        if (StringUtils.isNotEmpty(caseId)) {
            Example queryExample = new Example(CaseHandlePersonRecord.class);
            queryExample.createCriteria().andEqualTo("caseId", caseId).andEqualTo("status",Constant.STRING_1);
            queryExample.setOrderByClause("handle_person_type asc");
            List<CaseHandlePersonRecord> caseHandlePersonRecordList = caseHandlePersonRecordMapper.selectByExample(queryExample);
            if (caseHandlePersonRecordList.size() > 0) {
                List<CaseHandlePersonRecordVo> caseHandlePersonRecordVoList = new ArrayList<>();
                caseHandlePersonRecordList.forEach(e -> {
                    CaseHandlePersonRecordVo caseHandlePersonRecordVo = new CaseHandlePersonRecordVo();
                    BeanUtils.copyProperties(e, caseHandlePersonRecordVo);
                    caseHandlePersonRecordVoList.add(caseHandlePersonRecordVo);
                });
                return caseHandlePersonRecordVoList;
            } else {
                throw new BusinessException("未匹配到办案人员记录");
            }
        } else {
            throw new BusinessException("非法传值，案件id为空！");
        }
    }

    @Override
    public List<CaseHandlePersonRecord> getCaseHandlePersonRecordList(String caseId) {
        Example queryExample = new Example(CaseHandlePersonRecord.class);
        queryExample.createCriteria().andEqualTo("caseId", caseId).andEqualTo("status", Constant.STRING_1);
        List<CaseHandlePersonRecord> caseHandlePersonRecordList = caseHandlePersonRecordMapper.selectByExample(queryExample);
        return caseHandlePersonRecordList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteCaseHandlePersonRecordByCaseId(String caseId) {
        boolean flag=false;
        List<CaseHandlePersonRecord> caseHandlePersonRecordList = getCaseHandlePersonRecordList(caseId);
        if (ObjectUtil.isEmpty(caseHandlePersonRecordList) && caseHandlePersonRecordList.size()==0){
            return true;
        }
        Example handleExample = new Example(CaseHandlePersonRecord.class);
        handleExample.createCriteria().andEqualTo("caseId", caseId);
        int count = caseHandlePersonRecordMapper.deleteByExample(handleExample);
        if (count>0){
            flag=true;
        }else {
            throw new BusinessException("删除办案人员失败");
        }
        return flag;
    }
}
